import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {
        // 数据库连接URL，假设数据库名为 usersDb，需确保实际数据库存在该库及对应的表
        private final static String URL = "jdbc:mysql://localhost:3306/usersDb?useSSL=false&serverTimezone=UTC";
        // 数据库用户名，根据实际情况修改
        private final static String ROOT = "root";
        // 数据库密码，根据实际情况修改
        private final static String PASSWORD = "123456";

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            // 先执行登录流程示例，也可扩展菜单选择功能
            login(scanner);

            // 以下为功能演示，可根据需求调整调用逻辑，比如做成菜单让用户选择
            // register(scanner);
            // deleteUserById(scanner);
            // updatePasswordById(scanner);

            scanner.close();
        }

        public static void login(Scanner scanner) {
            System.out.println("请输入用户名：");
            String name = scanner.next();
            System.out.println("请输入密码：");
            String pwd = scanner.next();

            try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
                 PreparedStatement preparedStatement = connection.prepareStatement("select * from users where username =? and password =?;")) {

                preparedStatement.setString(1, name);
                preparedStatement.setString(2, pwd);
                ResultSet resultSet = preparedStatement.executeQuery();

                if (resultSet.next()) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("登录失败");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作异常：" + e.getMessage());
            }
        }


        public static void register(Scanner scanner) {
            System.out.println("请输入注册用户名：");
            String name = scanner.next();
            System.out.println("请输入注册密码：");
            String pwd = scanner.next();

            try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
                 PreparedStatement preparedStatement = connection.prepareStatement("insert into users(username, password) values (?,?);")) {

                preparedStatement.setString(1, name);
                preparedStatement.setString(2, pwd);
                int affectedRows = preparedStatement.executeUpdate();
                if (affectedRows > 0) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("注册失败");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作异常：" + e.getMessage());
            }
        }


        public static void deleteUserById(Scanner scanner) {
            System.out.println("请输入要删除用户的 ID：");
            int id = scanner.nextInt();

            try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
                 PreparedStatement preparedStatement = connection.prepareStatement("delete from users where id =?;")) {

                preparedStatement.setInt(1, id);
                int affectedRows = preparedStatement.executeUpdate();
                if (affectedRows > 0) {
                    System.out.println("用户删除成功");
                } else {
                    System.out.println("未找到对应 ID 的用户，删除失败");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作异常：" + e.getMessage());
            }
        }

        public static void updatePasswordById(Scanner scanner) {
            System.out.println("请输入要修改密码的用户 ID：");
            int id = scanner.nextInt();
            System.out.println("请输入新密码：");
            String newPwd = scanner.next();

            try (Connection connection = DriverManager.getConnection(URL, ROOT, PASSWORD);
                 PreparedStatement preparedStatement = connection.prepareStatement("update users set password =? where id =?;")) {

                preparedStatement.setString(1, newPwd);
                preparedStatement.setInt(2, id);
                int affectedRows = preparedStatement.executeUpdate();
                if (affectedRows > 0) {
                    System.out.println("密码修改成功");
                } else {
                    System.out.println("未找到对应 ID 的用户，修改失败");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作异常：" + e.getMessage());
            }
        }
    }
